package com.ktjiaoyu.crm.util;

import com.ktjiaoyu.crm.Jedis.RedisPool;
import redis.clients.jedis.Jedis;

public class RedisUtil {
    /**
     * 设置key的有效期，单位是秒
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key, Long exTime) {
        Jedis jedis=null;
        Long result = null;
        try {
            jedis= RedisPool.getJedis();
            result = jedis.expire(key, exTime);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    public static String estEx(String key, Long exTime, String value) {
        Jedis jedis=null;
        String result = null;
        try {
            jedis= RedisPool.getJedis();
            result = jedis.setex(key, exTime, value);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }

    public static String set(String key,String value) {
        Jedis jedis=null;
        String result = null;
        try {
            jedis= RedisPool.getJedis();
            result = jedis.set(key, value);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnResource(jedis);

        return result;
        }
    }
    public static String get(String key) {
        Jedis jedis=null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.get(key);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }
    public static Long del(String key) {
        Jedis jedis=null;
        Long result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.del(key);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnResource(jedis);
        }
        return result;
    }
}

